<?php

namespace App\Console\Commands;

use App\Store\Models\Goods\GoodsStockList;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;

class CopyGoodsStockList extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:CopyGoodsStockList';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '定时备份商品表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Log::info('定时备份商品表开始');
        $this->copygoodsstocklist();
        Log::info('定时备份商品表结束');
        return 0;
    }

    public function copygoodsstocklist()
    {
        $oldTable = env('DB_PREFIX').(new GoodsStockList())->getTable();
        $newTable = $oldTable . '_' . date('Ymd');

        DB::statement("CREATE TABLE $newTable LIKE $oldTable");
        DB::statement("INSERT $newTable SELECT * FROM $oldTable");
    }
}
